Accessing Microsoft Access Database in VB.NET


In this article, I will show you how to access Microsoft database using OLEDB 
data provider
Connection String:

       The ADO model uses the Connection String property to indicate the

OLEDB provider to use to connect to data store, along with the details needed

to complete the data connection.

  A typical connection String has two parts:

 Provider :< Provider name>; Data Source=<Source Details>

 

 A valid connection for connecting to Microsoft Access database:

Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C: student.mdb

 

Student

Used namespace: Imports System

Imports System.Data

Imports System.Data.OleDb  Inserting data into MS Access Database by VB.NET 2005 

Private Sub insert_Click(ByVal sender As System.Object,ByVal

e As System.EventArgs) Handles Insert.Click       

 

Dim con As OleDbConnection

Dim sql,str As String

str = "Provider=Microsoft.Jet.oledb.4.0;Data  Source=

C:student.mdb;"

con = New OleDbConnection(str)

sql = "insert into students(name,roll,mobile,email)

values('" & sname.Text & "','" & roll.Text & "',

'" & mobile.Text & "','"& email.Text & "')" 

  Dim cmd As OleDbCommand   

   Try

      con.Open()

     cmd = New OleDbCommand(sql, con)

      cmd.ExecuteNonQuery() 

      sname.Text = "" 

      roll.Text = ""           

mobile.Text = ""

      email.Text = ""   

      MessageBox.Show("Insert Successfully")

      con.Close()   

  Catch ex As OleDbException   

    MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error") 

  Catch ex As Exception

    MsgBox(ex.Message, MsgBoxStyle.Critical, "General Error")   

  End Try  

End Sub

 

17 responses to this post.

  1. hi friend…. thanks this code.. im new to vb.net….
    im searchin here after a long time i got this code…

    its very clear to me..

    thank you…..

    Reply

  2. really it good, amazing website ,its easy to unterstand the new developer.

    thanks many

    Reply

  3. Posted by Sudheesh k v on November 3, 2008 at 6:47 am

    hi sabuj,
    its very cool code snipet which could be useful for beginners like me
    thanks dude

    Reply

  4. Posted by Invincibles on November 14, 2008 at 6:33 am

    Hi Sabuj,

    I thank you for this working code. I made some project that make use of the connection to the MSAccess database but I can’t figure it out until I tried your code.

    Cheers =)

    Reply

  5. If you fix a problem then u send problem ,try to solve that

    Reply

  6. Thank you to Sabujcse to provide the code to how to connect MSaccess to visual basic 2005..

    Thank you once again

    Reply

  7. Posted by Nutthawut on January 8, 2010 at 2:46 pm

    thank you very much
    from Aof Thailand *-*

    Reply

  8. Thanks you very much

    I am seeking this type of solutiion.

    thanks a lot..

    Reply

  9. Posted by nadel on August 10, 2011 at 3:46 am

    ty 🙂

    Reply

  10. Posted by francis on August 17, 2011 at 5:50 am

    thanks, this works…..nice site

    Reply

  11. Posted by Anandraj on September 27, 2011 at 7:01 am

    Really nice….. It is easy to understand for easy developer.
    Thank U…………

    Reply

  12. Posted by dani mfungo on June 12, 2012 at 1:09 am

    i do appreciate

    Reply

  13. Posted by arun on August 29, 2012 at 6:18 pm

    nice.. realy useful for me.. thanks for the provider

    Reply

  14. Realy very nice…………………

    Reply

  15. Posted by udaykumar on January 10, 2013 at 1:59 am

    thanx bro………..

    Reply

  16. Simply desire to say your article is as amazing.

    The clarity in your post is just great and i can assume you’re an expert on this subject. Fine with your permission let me to grab your RSS feed to keep updated with forthcoming post. Thanks a million and please carry on the rewarding work.

    Reply

Leave a comment